using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using University.Models;

namespace University.Controllers
{   
    public class MarksController : Controller
    {
		private readonly IMarkRepository markRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public MarksController() : this(new MarkRepository())
        {
            //IProfessorRepository professorsRepo = new ProfessorRepository();
            //List<Professor> professors = professorsRepo.All.ToList<Professor>();

            //List<SelectListItem> items = new List<SelectListItem>();
            //foreach (Professor p in professors)
            //{
            //    items.Add(new SelectListItem 
            //    { 
            //        Text = String.Concat(p.FirstName, " ", p.LastName), Value = p.ProfessorId.ToString() 
            //    });
            //}

            //items.Add(new SelectListItem
            //{
            //    Text = "Please select Professor", Value = "0", Selected = true
            //});

            //ViewBag.Professors = items;
        }

        public MarksController(IMarkRepository markRepository)
        {
			this.markRepository = markRepository;
        }

        //
        // GET: /Marks/

        public ViewResult Index()
        {
            return View(markRepository.All);
        }

        //
        // GET: /Marks/Details/5

        public ViewResult Details(int id)
        {
            return View(markRepository.Find(id));
        }

        //
        // GET: /Marks/Create

        public ActionResult Create()
        {
            var model = new Mark();


            return View(model);
        } 

        //
        // POST: /Marks/Create

        [HttpPost]
        public ActionResult Create(Mark mark)
        {
            if (ModelState.IsValid) {
                markRepository.InsertOrUpdate(mark);
                markRepository.Save();
                return RedirectToAction("Index");
            } else {
                foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
                {
                    int i = 0;

                }
				return View();
			}
        }
        
        //
        // GET: /Marks/Edit/5
 
        public ActionResult Edit(int id)
        {
             return View(markRepository.Find(id));
        }

        //
        // POST: /Marks/Edit/5

        [HttpPost]
        public ActionResult Edit(Mark mark)
        {
            if (ModelState.IsValid) {
                markRepository.InsertOrUpdate(mark);
                markRepository.Save();
                return RedirectToAction("Index");
            } else {
				return View();
			}
        }

        //
        // GET: /Marks/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(markRepository.Find(id));
        }

        //
        // POST: /Marks/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            markRepository.Delete(id);
            markRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                markRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

